package com.cj.webapp.user.controller;

import com.cj.webapp.account.pojo.ConstantParamter;
import com.cj.webapp.common.utils.MapDetail;
import com.cj.webapp.common.utils.http.HttpsUtil;
import com.cj.webapp.user.dao.UserDao;
import com.cj.webapp.user.dto.UserDto;
import com.cj.webapp.user.service.UserServiceImpl;
import org.apache.poi.util.SystemOutLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.applet.Main;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 * Created by dell on 2018/1/12.
 */

public class UserServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(UserServlet.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String source=request.getParameter("source");
        String role=request.getParameter("role");//角色
        UserDto dto=new UserDto();
        dto.setName(username.trim());
        dto.setPassword(password.trim());
        dto.setPhone(username.trim());
        dto.setSource(source);
        dto.setRole(role);

        UserServiceImpl serviceImpl=new UserServiceImpl();
        String num = serviceImpl.queryUser(dto);
        UserDao userDao=new UserDao();
        logger.info("用户输入的用户名："+username+"密码："+password);
        UserDto userDto = userDao.findUserByName(dto);


        if(userDto!=null){
            MapDetail.resMap.put("platfmCustId",userDto.getId());
        }
        logger.info("查询用户返回结果0："+num+"+进入home页面,否则进入error页面");
        if(num.equals("0")){
            //获取token
            String urlString = ConstantParamter.DIRECT_WAY_URL;
            HttpsUtil httpsUtil=new HttpsUtil();
            Map<String,Object> map=new HashMap<>();
            String sessionId = session.getId();

            MapDetail.resMap.put("sessionId",sessionId);
            MapDetail.resMap.put("source",source);
            MapDetail.resMap.put("role",role);//角色


            map.put("sessionId",sessionId);
            map.put("platfmCustId",userDto.getId());
            map.put("custNm",userDto.getName());
            map.put("source",source);
            String token = httpsUtil.sendToken(map, urlString);
            if(token!=null&&!token.equals("")){
                MapDetail.resMap.put("token",token);
            }
            //request.setAttribute("platfmCustId",userDto.getId());

            request.getRequestDispatcher("page/index/home.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("page/index/error.html").forward(request,response);
        }
        //resMap.put("userId",userDto.getId());
        MapDetail.resMap.put("userName",userDto.getName());
        MapDetail.resMap.put("phone",userDto.getPhone());


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              doPost(request,response);
    }


}
